<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <router-link to="/" class="logo-link">
          <img src="../../assets/logo.svg" alt="柯基工具箱" class="logo-image" />
          <h1 class="logo-text">柯基工具箱</h1>
        </router-link>
      </div>
      
      <LoginForm
        @login-success="handleLoginSuccess"
        @switch-mode="goToRegister"
      />
      
      <div class="login-footer">
        <p>还没有账号？<router-link to="/register" class="register-link">立即注册</router-link></p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import LoginForm from '../../components/user/LoginForm.vue';
import { onMounted } from 'vue';

const router = useRouter();
const route = useRoute();

// 检查是否有登录提示消息，或是否从注册页面跳转过来
onMounted(() => {
  // 显示登录提示消息
  const loginMessage = sessionStorage.getItem('loginMessage');
  if (loginMessage) {
    ElMessage.warning(loginMessage);
    sessionStorage.removeItem('loginMessage');
  }
  
  // 如果是从注册页面跳转过来的，显示欢迎消息
  if (route.query.from === 'register') {
    ElMessage({
      message: '欢迎！请使用刚刚注册的账号登录',
      type: 'success',
      duration: 3000
    });
  }
});

// 登录成功处理
const handleLoginSuccess = () => {
  ElMessage.success('登录成功，欢迎回来！');
  
  // 检查是否有登录后操作
  // 如果是从注册页面来的，直接跳转到首页
  if (route.query.from === 'register') {
    router.push('/');
    return;
  }
  
  // 否则检查是否有重定向路径
  const redirectPath = route.query.redirect || sessionStorage.getItem('redirectPath');
  sessionStorage.removeItem('redirectPath');
  
  if (redirectPath) {
    // 如果有重定向路径，跳转到指定页面
    router.push(redirectPath);
  } else if (sessionStorage.getItem('postLoginAction') === 'upgrade') {
    // 跳转到会员页面
    sessionStorage.removeItem('postLoginAction');
    router.push('/membership');
  } else {
    // 返回上一页，如果没有则去首页
    if (history.state.back) {
      router.back();
    } else {
      router.push('/');
    }
  }
};

// 切换到注册页面
const goToRegister = () => {
  router.push('/register');
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 40px 20px;
}

.login-container {
  width: 100%;
  max-width: 550px;
  animation: fadeIn 0.8s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 50px;
}

.logo-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--black);
  transition: transform 0.3s ease;
}

.logo-link:hover {
  transform: scale(1.05);
}

.logo-image {
  height: 48px;
  margin-right: 16px;
}

.logo-text {
  font-size: 28px;
  font-weight: 700;
  color: var(--black);
  letter-spacing: 0.5px;
}

.login-footer {
  text-align: center;
  margin-top: 40px;
  color: var(--dark-gray);
  font-size: 16px;
}

.register-link {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 600;
  transition: all 0.3s;
  position: relative;
  padding-bottom: 2px;
}

.register-link:after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: 0;
  left: 0;
  background-color: var(--primary-color);
  transition: width 0.3s ease;
}

.register-link:hover {
  color: var(--primary-dark);
}

.register-link:hover:after {
  width: 100%;
}

@media (max-width: 768px) {
  .login-container {
    max-width: 100%;
  }
  
  .logo-image {
    height: 40px;
  }
  
  .logo-text {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .login-page {
    padding: 20px 16px;
  }
  
  .login-header {
    margin-bottom: 30px;
  }
  
  .logo-image {
    height: 36px;
  }
  
  .logo-text {
    font-size: 22px;
  }
}
</style> 